import createHandler from './index'
import {insertOne, selectOne} from "./fake-db"
import {now} from "../utils/time"
import {v4} from "uuid"
import encrypt from "../utils/encrypt"

/**
 *  code: 1000-2000
 */


const register = createHandler(({uid, name, email, password}) => {
    if (selectOne('user', 'uid', uid)) {
        return {
            code: 1006,
            msg: '该账号已注册'
        }
    }
    if (selectOne('user', 'email', email)) {
        return {
            code: 1007,
            msg: '该邮箱已注册'
        }
    }
    const user = {
        id: v4(),
        uid,
        name,
        email,
        password: encrypt(password),
        role: null,
        created_at: now(),
        last_login_at: "1/1/1 00:00:00",
        enable: true
    }
    insertOne('user', user)

    return {code: 0}
})

export {register}